//
// Created by Administrator on 2023/6/3.
//


#include "../header/Data_Science.h"


Data_Science::Data_Science(){}


Data_Science::Data_Science(QVector<double> * qvec_data): qvec_data(qvec_data)
{
    this->vec_mode = this->Mode(this->qvec_data);
    this->vec_mean = this->Mean(this->qvec_data);
    this->vec_var = this->Var(this->qvec_data);
    this->vec_std = this->Std(this->qvec_data);


    this->vec_max = this->Vec_Max(this->qvec_data);
    this->vec_mini = this->Vec_Mini(this->qvec_data);
    this->vec_range = this->Vec_Range(this->qvec_data);
    this->vec_median = this->Vec_Median(this->qvec_data);
}


Data_Science::~Data_Science()
{}


double Data_Science::Mode(QVector<double> *qvec_data)
{
    QMap<double, int> temp_map;

    for(auto it: *qvec_data)
    {
        temp_map[it] += 1;
    }


    int temp_max = 0;
    double max_num = 0;
    for (QMap<double, int>::const_iterator it = temp_map.constBegin(); it != temp_map.constEnd(); it++)
    {
        if(temp_max < it.value())
        {
            temp_max = it.value();
            max_num = it.key();
        }
    }

    return max_num;
}


double Data_Science::Mean(QVector<double> *qvec_data)
{
    double sum_data {0};
    for(auto it: *qvec_data)
    {
        sum_data += it;
    }
    return sum_data / qvec_data->size();
}


double Data_Science::Var(QVector<double> *qvec_data)
{
    double sum_data {0};
    for(auto it: *qvec_data)
    {
        sum_data += pow((it - this->Mean(qvec_data)), 2);
    }
    return sum_data / qvec_data->size();
}


double Data_Science::Std(QVector<double> *qvec_data)
{
    double var = this->Var(qvec_data);
    return pow(var, 0.5);
}


double Data_Science::Vec_Max(QVector<double> *qvec_data)
{
    double temp_max = (*qvec_data)[0];
    for(auto it: *qvec_data)
    {
        if(temp_max < it)
        {
            temp_max = it;
        }
    }
    return temp_max;
}


double Data_Science::Vec_Mini(QVector<double> *qvec_data)
{
    double temp_mini = (*qvec_data)[0];
    for(auto it: *qvec_data)
    {
        if(temp_mini > it)
        {
            temp_mini = it;
        }
    }
    return temp_mini;
}


double Data_Science::Vec_Range(QVector<double> *qvec_data)
{
    double rang = this->Vec_Max(qvec_data) - this->Vec_Mini(qvec_data);
    return rang;
}


double Data_Science::Vec_Median(QVector<double> *qvec_data)
{
    std::sort(qvec_data->begin(), qvec_data->end());
    int parity = qvec_data->size() % 2;
    int temp_median = 0;
    switch (parity)
    {
        case 0:
            temp_median = ((*qvec_data)[qvec_data->size() / 2] + (*qvec_data)[qvec_data->size() / 2 - 1]) / 2;
            break;
        case 1:
            temp_median = (*qvec_data)[qvec_data->size() / 2];

    }

    return temp_median;
}
